/**
 * 
 */
package pl.com.like.sequoia.jms.connection;

import java.io.Serializable;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import pl.com.sequoia.jms.PdbCompareMessage;
import pl.com.sequoia.jms.helper.IJmsPdbCompareHandler;

/**
 * @author pwolanski
 * 
 */
@Component
public class JmsCompareMessageListener implements MessageListener {

	private final static Logger log = LoggerFactory.getLogger(JmsCompareMessageListener.class);
	private IJmsPdbCompareHandler compareHandler;

	public IJmsPdbCompareHandler getCompareHandler() {
		return compareHandler;
	}

	/** {@inheritDoc} */
	public void onMessage(Message message) {
		try {
			if (log.isDebugEnabled()) {
				log.debug("new compare message arrived!");
			}

			if (message instanceof ObjectMessage) {
				ObjectMessage mMsg = (ObjectMessage) message;
				Serializable messageObj = mMsg.getObject();
				if (messageObj instanceof PdbCompareMessage) {
					if (log.isDebugEnabled()) {
						log.debug("new message have set correct pdb compare message object {}", messageObj);
					}
					PdbCompareMessage pdbCompareObj = (PdbCompareMessage) messageObj;
					compareHandler.compare(pdbCompareObj);
				}
			}
		} catch (JMSException e) {
			log.error(e.getMessage(), e);
		}

	}

	public void setCompareHandler(IJmsPdbCompareHandler compareHandler) {
		this.compareHandler = compareHandler;
	}

}
